unit DBEngine;

interface
uses
  Classes,SysUtils,ADODB,XMLDOC, XMLIntf,Class_DBPool;

type  
  TDBEngine = class;
  TDBEngineClass =class of TDBEngine;
  TOperateType   =(otNone, otAdd, otEdit, otDel);

  TXMLEngine=class(TObject)
  public
    class function ReadFromXML(ANode: IXMLNode): TDBEngine;virtual;
    function WriteToXML(ANode: IXMLNode): IXMLNode;virtual;

    class function LoadFromStream(AStream: TStream): TDBEngine;virtual;
    procedure SaveToStream(AStream: TStream);virtual;

    class function LoadFromFile(AFileName: string): TDBEngine;virtual;
    procedure SaveToFile(AFileName: string);virtual;
  end;

  TDBEngine=class(TXMLEngine)
  public
    FStrSQL:string;
    FADOCmd:TADOCommand;
    FOpType:TOperateType;
  protected
    function  GetADOCommand:TADOCommand;
  protected
    procedure SetParameters;virtual;abstract;
    function  GetStrInsert:string;virtual;abstract;
    function  GetStrUpdate:string;virtual;abstract;
    function  GetStrDelete:string;virtual;abstract;
  public
    procedure InsertDB(AADOCon:TADOConnection);overload;virtual;
    procedure UpdateDB(AADOCon:TADOConnection);overload;virtual;
    procedure DeleteDB(AADOCon:TADOConnection);overload;virtual;
    procedure DeleteFL(AADOCon:TADOConnection);overload;virtual;

    procedure InsertDB;overload;virtual;
    procedure UpdateDB;overload;virtual;
    procedure DeleteDB;overload;virtual;
    procedure DeleteFL;overload;virtual;
  public
    class function ReadDS(ADODS: TADODataSet): TDBEngine;virtual;abstract;
    class function StrsDB(ASQL:string;AADOCon:TADOConnection):TStringList;virtual;
  public
    property  ADOCmd:TADOCommand read GetADOCommand;
  end;

implementation

uses
  Dialogs;


{ TXMLEngine }

class function TXMLEngine.LoadFromFile(AFileName: string): TDBEngine;
begin

end;

class function TXMLEngine.LoadFromStream(AStream: TStream): TDBEngine;
begin

end;

class function TXMLEngine.ReadFromXML(ANode: IXMLNode): TDBEngine;
begin

end;

procedure TXMLEngine.SaveToFile(AFileName: string);
begin

end;

procedure TXMLEngine.SaveToStream(AStream: TStream);
begin

end;

function TXMLEngine.WriteToXML(ANode: IXMLNode): IXMLNode;
begin

end;

{ TDBEngine }


procedure TDBEngine.DeleteDB(AADOCon: TADOConnection);
begin
  try
    FStrSQL:=GetStrDelete;
    if FStrSQL='' then raise Exception.Create('you must be override getstrinsert method');
    FOpType:=otDel;
    ADOCmd.Connection:=AADOCon;
    FADOCmd.Prepared :=True;
    FADOCmd.CommandText:=FStrSQL;
    SetParameters;
    try
      FADOCmd.Execute;
    except
      On E:Exception do
      begin
        raise Exception.Create(E.Message);
      end;
    end;
  finally
    FreeAndNil(FADOCmd);
  end;
end;
procedure TDBEngine.DeleteFL(AADOCon: TADOConnection);
begin

end;


procedure TDBEngine.DeleteFL;
begin

end;

function TDBEngine.GetADOCommand: TADOCommand;
begin
  if FADOCmd=nil then
  begin
    FADOCmd := TADOCommand.Create(nil);
  end;
  Result:= FADOCmd;
end;

procedure TDBEngine.InsertDB(AADOCon: TADOConnection);
begin
  try
    FStrSQL:=GetStrInsert;
    if FStrSQL='' then raise Exception.Create('you must be override getstrinsert method');
    FOpType:=otAdd;
    ADOCmd.Connection:=AADOCon;
    FADOCmd.Prepared :=True;
    FADOCmd.CommandText:=FStrSQL;
    SetParameters;
    try
      FADOCmd.Execute;
    except
      On E:Exception do
      begin
        raise Exception.Create(E.Message);
      end;  
    end;
  finally
    FreeAndNil(FADOCmd);
  end;
end;

procedure TDBEngine.InsertDB;
var
  ADOCon:TADOConnection;
begin
  try
    ADOCon:=TDBPool.GetConnect();
    InsertDB(ADOCon);
  finally
    FreeAndNil(ADOCon);
  end;
end;

class function TDBEngine.StrsDB(ASQL: string;
  AADOCon: TADOConnection): TStringList;
var
  ADODS:TADODataSet;
  AObj :TDBEngine;
begin
  Result:=nil;
  try
    ADODS:=TADODataSet.Create(nil);
    ADODS.Prepared   :=True;
    ADODS.Connection :=AADOCon;
    if ASQL='' then raise Exception.Create('SQL=NIL');
    ADODS.CommandText:=ASQL;
    ADODS.Open;
    if ADODS.RecordCount=0 then Exit;
    Result:=TStringList.Create;
    while not ADODS.Eof do
    begin
      AObj:=ReadDS(ADODS);
      Result.AddObject('',AObj);
      ADODS.Next;
    end;      
  finally
    FreeAndNil(ADODS);
  end;
end;

procedure TDBEngine.UpdateDB(AADOCon: TADOConnection);
begin
  try
    FStrSQL:=GetStrUpdate;
    if FStrSQL='' then raise Exception.Create('you must be override getstrupdate method');
    FOpType:=otEdit;
    ADOCmd.Connection:=AADOCon;
    FADOCmd.Prepared :=True;
    FADOCmd.CommandText:=FStrSQL;
    SetParameters;
    try
      FADOCmd.Execute;
    except
      On E:Exception do
      begin
        raise Exception.Create(E.Message);
      end;  
    end;
  finally
    FreeAndNil(FADOCmd);
  end;
end;
procedure TDBEngine.DeleteDB;
var
  ADOCon:TADOConnection;
begin
  try
    ADOCon:=TDBPool.GetConnect();
    DeleteDB(ADOCon);
  finally
    FreeAndNil(ADOCon);
  end;
end;

procedure TDBEngine.UpdateDB;
var
  ADOCon:TADOConnection;
begin
  try
    ADOCon:=TDBPool.GetConnect();
    UpdateDB(ADOCon);
  finally
    FreeAndNil(ADOCon);
  end;
end;
end.
